/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.charge;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.charge.ChargeRecord;
import com.server.modules.sys.service.charge.ChargeRecordService;

/**
 * 充值记录查询Controller
 * @author 王磊
 * @version 2016-09-21
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/charge/chargeRecord")
public class ChargeRecordController extends BaseController {

	@Autowired
	private ChargeRecordService chargeRecordService;

	@ModelAttribute
	public ChargeRecord get(@RequestParam(required=false) String id) {
		ChargeRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = chargeRecordService.get(id);
		}
		if (entity == null){
			entity = new ChargeRecord();
		}
		return entity;
	}

	@RequiresPermissions("sys:charge:chargeRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(ChargeRecord chargeRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ChargeRecord> page = chargeRecordService.findPage(new Page<ChargeRecord>(request, response), chargeRecord);
		model.addAttribute("page", page);
		model.addAttribute("chargeRecord", chargeRecord);
		return "modules/sys/charge/chargeRecordList";
	}

	@RequiresPermissions("sys:charge:chargeRecord:view")
	@RequestMapping(value = "form")
	public String form(ChargeRecord chargeRecord, Model model) {
		model.addAttribute("chargeRecord", chargeRecord);
		return "modules/sys/charge/chargeRecordForm";
	}

	@RequiresPermissions("sys:charge:chargeRecord:edit")
	@RequestMapping(value = "save")
	public String save(ChargeRecord chargeRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, chargeRecord)){
			return form(chargeRecord, model);
		}
		chargeRecordService.save(chargeRecord);
		addMessage(redirectAttributes, "保存充值记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/charge/chargeRecord/?repage";
	}

	@RequiresPermissions("sys:charge:chargeRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(ChargeRecord chargeRecord, RedirectAttributes redirectAttributes) {
		chargeRecordService.delete(chargeRecord);
		addMessage(redirectAttributes, "删除充值记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/charge/chargeRecord/?repage";
	}

}